package com.yishusmt.wechatapi.wechatdev.utils;

import com.yishusmt.wechatapi.wechatdev.constant.RandConstant;

import java.util.Arrays;

/**
 * 随机数生成
 *
 * @author zhangyi
 * @date 2018/9/1 15:12
 */
public class RandomUtil {

    /**
     * 随机数生成
     *
     * @return
     */
    public static String randomCode(){
        String codeStr = "1234567890";
        StringBuffer result = new StringBuffer();
        for(int i = 0; i < RandConstant.RAND_LENGTH;i++){
            char[] chars = codeStr.toCharArray();
            Arrays.asList(chars).stream()
                    .forEach(e -> {
                        Integer codeIndex = (int)Math.floor(Math.random()*9);
                        result.append(chars[codeIndex]);
                    });
        }
        return result.toString();
    }

    /**
     * 验证码
     *
     * @param size
     * @return
     */
    public static String randomCode(Integer size){
        String codeStr = "1234567890";
        StringBuffer result = new StringBuffer();
        for(int i = 0; i < size;i++){
            char[] chars = codeStr.toCharArray();
            Arrays.asList(chars).stream()
                    .forEach(e -> {
                        Integer codeIndex = (int)Math.floor(Math.random()*9);
                        result.append(chars[codeIndex]);
                    });
        }
        return result.toString();
    }
}
